<?php 
 
namespace App\Http\Controllers\Admin; 
 
use App\Http\Controllers\Controller; 
use Illuminate\Http\Response;
use Illuminate\Http\Request;
use App\Http\Requests\PermissionRequest;
use App\Permission;

class PermissionController extends Controller 
{ 
    /** 
     * Create a new controller instance. 
     * 
     * @return void 
     */ 

    // 
    const model="PersonnelManagement";
    //
    public function __construct(){
        $this->model=self::model;
    }

    public function index() 
    {   
        $permission=Permission::sortId('desc')->paginate(15);
        return view('admin.permission.index',['model'=>$this->model,'permission'=>$permission]);
    }

    public function create() 
    {   

        $Permission=Permission::orderBy('id','desc')->where('cid','0')->get()->toArray();
        $tree=make_tree($Permission);
        return view('admin.permission.create',['tree'=>$tree,'model'=>$this->model]);
    }

    public function store(PermissionRequest $request){

        $data=$request->intersect('name','display_name','description','cid');

        try {
            Permission::create($data);
        } catch (Exception $e) {    
        return   parent::error('error');
        }
        return parent::success('success',route('admin.permission.index'));
    }

    public function del($id){

        try {
            Permission::destroy($id);
        } catch (Exception $e) {    
            return   parent::error('error');
        }
        return parent::success('success',route('admin.permission.index'));

    }

    public function edit($id){
        $Permission=Permission::orderBy('id','desc')->where('cid','0')->get()->toArray();
        $tree=make_tree($Permission);
        $per=Permission::find($id);
        
        return view('admin.permission.edit',['per'=>$per,'tree'=>$tree,'model'=>$this->model]);
    }

    public function update(Request $request){
        $id=$request->input('id');
        $data=$request->intersect('name','display_name','description','cid');

        try {
            Permission::where('id',$id)->update($data);
        } catch (Exception $e) {    
        return   parent::error('error');
        }

        return parent::success('success',route('admin.permission.index'));
    }

}
